
<?php
include("dataprovider.php");
include ("Cart.php");
?>
<?php include("includes/function.php") ?>
<?php include("includes/header.php") ?>

<div class="container">
<span class="title">Quản lí giỏ hàng</span>
    <div id="my-post">
        <div class="my-post-entry">
        <form action="ViewCart.php"  method="post" name="myform">
            <table class="table-cart" border="1">
                <tr>
                    <th width="300">Tên sản phẩm</th>
                    <th width="80">Số lượng</th>
                    <th width="150">Đơn giá</th>
                    <th width="200">Thành tiền</th>
                    <th width="40">&nbsp;</th>
                    <th width="40">&nbsp;</th>
                </tr>
                <?php
                if (isset($_SESSION["cartname"]) == true) {
                    if (isset($_REQUEST["delete"])) {
                        $_SESSION["cartname"]->DeleteProduct($_REQUEST["delete"]);
                    }
                    if (isset($_REQUEST["txtProductPrice"])) {
                        $_SESSION["cartname"]->EditProduct($_REQUEST["update"], $_REQUEST["txtProductPrice"]);
                    }
                    if(isset($_REQUEST["paybill"]) == true){
                        $n = $_SESSION["customername"];
                        $t = $_SESSION["cartname"]->SumMoney();
                        $query = "insert into Cart values('','$n',$t)";
                        $r = mysqli_query($dbc, $query);
                        confirm_query($r, $query);
                        $query = "select * from Cart where CartID = (select MAX(CartID) from Cart)";
                        $r = mysqli_query($dbc, $query);
                        confirm_query($r, $query);
                        $row = mysqli_fetch_array($r, MYSQL_ASSOC);
                        $s = $row['CartID'];
                        foreach ($_SESSION["cartname"]->getProductList() as $value) {                             
                        $p = $value->getProductID();
                        $sl = $value->getQuantity();
                        $query = "insert into Detail values('','$s','$p','$sl')";
                        $r = mysqli_query($dbc, $query);
                        confirm_query($r, $query);
                        $_SESSION["cartname"]->DeleteProduct($value->getProductID());
                        }
                    }
                    foreach ($_SESSION["cartname"]->getProductList() as $value) {
                        ?>
                        <tr align="center"  valign="middle">
                            <td><?php echo $value->getProductName(); ?></td>
                            <td>
                                <?php
                                if (isset($_REQUEST["edit"])) {
                                    if ($value->getProductID() == $_REQUEST["edit"]) {
                                        ?>
                                        <input  name="txtProductPrice"  type="text" size="5" value="<?php echo $value->getQuantity(); ?>"/>
                                        <?php
                                    } else {
                                        echo $value->getQuantity();
                                    }
                                } else {
                                    echo $value->getQuantity();
                                }
                                ?>
                            </td>
                            <td><?php echo number_format($value->getProductPrice()). " VNĐ" ?></td>
                            <td><?php echo number_format($value->getSum()). " VNĐ" ?></td> 
                            <td>
                                <?php
                                if (isset($_REQUEST["edit"])) {
                                    if ($value->getProductID() == $_REQUEST["edit"]) {
                                        ?>
                                        <input  name="update"  type="hidden"  value="<?php echo $value->getProductID(); ?>"/>               
                                        <input name="btnUpdate" type="submit"  value="Cập nhật" class="btn-update" />
                                        <?php
                                    } else {
                                        ?>
                                        <a  href="ViewCart.php&edit=<?php echo $value->getProductID(); ?>">Sửa</a>
                                        <?php
                                    }
                                } else {
                                    ?>
                                    <a  href="ViewCart.php?edit=<?php echo $value->getProductID(); ?>">Sửa</a>
                                <?php }
                                ?> 
                            </td>
                            <td>
                                <?php
                                if (isset($_REQUEST["edit"])) {
                                    if ($value->getProductID() == $_REQUEST["edit"]) {
                                        ?>
                                        <a href="ViewCart.php?">Hủy</a>       
                                        <?php
                                    } else {
                                        ?>
                                        <a  href="ViewCart.php?delete=<?php echo $value->getProductID(); ?>">Xóa</a>
                                        <?php
                                    }
                                } else {
                                    ?>
                                    <a  href="ViewCart.php?delete=<?php echo $value->getProductID(); ?>">Xóa</a>
                                <?php }
                                ?> 
                            </td>
                        </tr> 
                        <?php
                    }
                }
                ?>
            </table>
            <table align="right" style="margin-top:10px;" class="table-cart1">
                <tr>
                    <td align="right" style="padding-right:10px;"><strong>Tổng số tiền:</strong></td> 
                    <td style="text-align:left; color:red"><?php
                        if (isset($_SESSION["cartname"]) == true)
                            echo number_format($_SESSION["cartname"]->SumMoney()) . " VNĐ";
                        ?></td>
                </tr>
                <tr>
                    <td align="right" style="padding-right:10px;"><strong>Tổng sản phẩm mua:</strong></td>
                    <td style="text-align:left; color:red"><?php
                        if (isset($_SESSION["cartname"]) == true)
                            echo
                            $_SESSION["cartname"]->SumProduct();
                        ?></td>
                </tr>
                <tr align="right" valign="middle">
                    <td  colspan="2"  align="center"><a  href="ViewCart.php?paybill=1">Thanh toán</a></td>
                </tr>
            </table>
        </form>
        </div>
    </div>
</div>
    <?php include("includes/footer.php") ?>